from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from persons.models import Person
from persons.serializers import PersonBriefSerl, PersonDetSerl
from student.models import StudentInvite, StudentInvitePerson
from teacher.auth import TeacherJWT
from teacher.models import TeacherInvite, TeacherInvitePerson
from visitor.models import VisitorApply, VisitorApplyPerson


class DetApi(APIView):
    permission_classes = (IsAuthenticated,)
    authentication_classes = [TeacherJWT]

    def get(self, request, format=None):
        result = {"code": 0, "msg": "ok", "data": {}}
        uuid = request.GET.get("uuid")
        person = Person.objects.get(uuid=uuid)
        result["data"]["person"] = PersonDetSerl(person).data
        return Response(result)


class InfoListApi(APIView):

    def get(self, request, format=None):
        result = {"code": 0, "msg": "ok", "data": {}}
        kind = request.GET.get("kind")
        id = request.GET.get("id")
        if kind == "visitor":
            visitor_apply = VisitorApply.objects.get(id=id)
            result["data"]["jobDate"] = visitor_apply.when.strftime("%Y%m%d")
            apply_person_list = VisitorApplyPerson.objects.filter(apply=visitor_apply)
            person_list = Person.objects.filter(uuid__in=[apply_person.person.uuid for apply_person in apply_person_list])
            result["data"]["visitorList"] = PersonDetSerl(person_list,many=True).data
        elif kind == "student":
            student_invite = StudentInvite.objects.get(id=id)
            result["data"]["jobDate"] = student_invite.when.strftime("%Y%m%d")
            invite_person_list = StudentInvitePerson.objects.filter(invite=student_invite)
            person_list = Person.objects.filter(uuid__in=[invite_person.person.uuid for invite_person in invite_person_list])
            result["data"]["visitorList"] = PersonDetSerl(person_list,many=True).data
        elif kind == "teacher":
            teacher_invite = TeacherInvite.objects.get(id=id)
            result["data"]["jobDate"] = teacher_invite.when.strftime("%Y%m%d")
            invite_person_list = TeacherInvitePerson.objects.filter(invite=teacher_invite)
            person_list = Person.objects.filter(uuid__in=[invite_person.person.uuid for invite_person in invite_person_list])
            result["data"]["visitorList"] = PersonDetSerl(person_list,many=True).data
        return Response(result)